class Node:
    def __init__(self,item):
        self.item=item
        self.next=None
class StackIterator:
    def __init__(self,stack):
        self.stack=stack
        self.cursor=self.stack.head
    def __next__(self):
        if self.cursor == None:
            raise StopIteration
        else:
            item=self.cursor.item
            self.cursor=self.cursor.next
            return item
class Stack:
    def __init__(self):
        self.head=None
    def push(self,item):
        node=Node(item)
        node.next=self.head
        self.head=node
    def __iter__(self):
        return StackIterator(self)
stack=Stack()
stack.push('a')
stack.push('b')
stack.push('c')
stackIterator=iter(stack)
while True:
    try:
        item=next(stackIterator)
        print(item)
    except StopIteration:
        break
for item in stack:
    print(item)
